'use strict';
const fs = require('fs');
const moment = require('moment'); // moment.js
const html = fs.readFileSync('./template/test.html', 'utf8'); // 引入html模板
const createPdf = require('../../util/create'); // 引入写好的函数

const Controller = require('egg').Controller;

class ProductController extends Controller {
  async create() {
    const { ctx } = this;
    console.log(ctx.request.body);
    const {name, age} = ctx.request.body;
    ctx.body = {
      name,
      age
    }

    var options = {
        "format": 'A4',
        "header": {
          "height": "10mm",
          "contents": ''
        },
        "orientation" : "portrait" ,  // 纵向或横向
        "border": {
          "top": "2in",            // default is 0, units: mm, cm, in, px
          "right": "1in",
          "bottom": "2in",
          "left": "1.5in"
        },
        "paginationOffset" : 1 
    }; // 一些配置
    // var name = '张三';
    // 匹配规则
    var reg = [
      {
        relus: /__name__/g,
        match: name
      },
      {
        relus: /__date__/g,
        match: age  //moment().format('YYYY年MM月DD日')
      }
    ];
    const path = './pdf/'
    const fileName = `test${moment().format('YYYYMMDDhmmss')}`
    const ext = '.pdf'
    createPdf.createPDFProtocolFile(html, options, reg, `${path}${fileName}${ext}`); // 传入参数 生成pdf

  }

 

}

module.exports = ProductController;
